package com.ljfiction.book.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ljfiction.book.R;

/**
 * Created by Android on 2017/11/10.
 */

public class FabuzhangjieIsVIP {
    private Dialog mDialog;
    private FabuzhangjieIsVIP.Listener listener;
    private RelativeLayout isvip;
    private RelativeLayout novip;
    private ImageView isvipImage;
    private ImageView novipImage;
    private TextView ok;
    private ImageView delet;
    private int isVip = 1;//0是非VIP，1是VIP

    public FabuzhangjieIsVIP(Context context, final int isbookvip, boolean isSelectClose) {
        mDialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        View view = LayoutInflater.from(context).inflate(R.layout.fabuzhangjie_vipdialog, null);
        //将布局设置给Dialog
        mDialog.setContentView(view);
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = context.getResources().getDisplayMetrics().widthPixels;
        view.setLayoutParams(layoutParams);
        mDialog.getWindow().setGravity(Gravity.CENTER);
        mDialog.getWindow().setWindowAnimations(R.style.ActionSheetDialogAnimation);
        mDialog.setCancelable(isSelectClose);
        isvip = mDialog.findViewById(R.id.setvip);
        novip = mDialog.findViewById(R.id.setnovip);
        isvipImage =  mDialog.findViewById(R.id.isvip_select);
        novipImage =  mDialog.findViewById(R.id.isvip_noselect);
        ok =  mDialog.findViewById(R.id.isokfabu);
        delet=mDialog.findViewById(R.id.delet_fabuvip);
        if (isbookvip == 0) {
            isVip = 0;
            isvipImage.setImageResource(R.mipmap.normal);
            novipImage.setImageResource(R.mipmap.select);
        }
        isvip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (isbookvip == 0) {
                    if (listener != null) {
                        listener.isOkClick();
                    }
                    return;
                }
                isVip = 1;
                isvipImage.setImageResource(R.mipmap.select);
                novipImage.setImageResource(R.mipmap.normal);
            }
        });

        novip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isVip = 0;
                isvipImage.setImageResource(R.mipmap.normal);
                novipImage.setImageResource(R.mipmap.select);

            }
        });
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.okClick(isVip);
                    mDialog.dismiss();
                }
            }
        });
        delet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.isdelect();
                    mDialog.dismiss();
                }
            }
        });

    }


    public void show() {
        if (mDialog != null) {
            mDialog.show();
        }
    }

    public void setListener(Listener listener) {
        this.listener = listener;
        show();
    }


    public interface Listener {
        public void okClick(int isVip);

        public void isOkClick();
        public void isdelect();

    }
}
